package com.newland.starpos.partner.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.nio.charset.Charset;
import java.security.cert.X509Certificate;


/**
 * Created by liuNW on 2018/9/28.
 */
@Slf4j
public class HttpUtils {

    private static HttpComponentsClientHttpRequestFactory requestFactory;

    static { // 这里引了httpclient 忽略https证书问题
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

        SSLContext sslContext = null;
        try {
            sslContext = SSLContextBuilder.create().loadTrustMaterial(acceptingTrustStrategy)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();

        requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    }

    public static String postJsonU8(String url, String content) {

        RestTemplate client = new RestTemplate(requestFactory);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);


        HttpEntity<String> entity = new HttpEntity<>(content, headers);
        log.info("request uri : " + url);
        log.info("request header : {}", headers);
        log.info("request body : {}", content);
        ResponseEntity<String> response = client.postForEntity(url, entity, String.class);
        log.info("response statusCode : {}, body : {}",
                response.getStatusCode().value(), response.getBody());
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        }

        throw new RestClientResponseException("远程调用出错", response.getStatusCodeValue(),
                response.getStatusCode().toString(), response.getHeaders(),
                response.getBody().getBytes(), Charset.defaultCharset());
    }


    public static String postJsonGBK(String url, String content) {
        RestTemplate client = new RestTemplate(requestFactory);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.set


        HttpEntity<String> entity = new HttpEntity<>(content, headers);
        log.info("request uri : " + url);
        log.info("request header : {}", headers);
        log.info("request body : {}", content);
        ResponseEntity<String> response = client.postForEntity(url, entity, String.class);
        log.info("response statusCode : {}, body : {}",
                response.getStatusCode().value(), response.getBody());
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        }

        throw new RestClientResponseException("远程调用出错", response.getStatusCodeValue(),
                response.getStatusCode().toString(), response.getHeaders(),
                response.getBody().getBytes(), Charset.defaultCharset());
    }
}
